// 本模块为首页视图相关逻辑及状态,主要用来做页面传参
import match from '@/api/match.js'

const state = {
    matchList: [],
    matchDetails: {},
    aboutMatchList: [],
    aboutMatchDetails: {},
}

const getters = {
    matchList(state) {
        return state.matchList
    },
    matchDetails(state) {
        return state.matchDetails
    },
    aboutMatchList(state) {
        return state.aboutMatchList
    },
    aboutMatchDetails(state) {
        return state.aboutMatchDetails
    },
}

const mutations = {
    setMatchState(state, { key, value }) {
        state[key] = value
    },
}

const actions = {
    getMatchList({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            match.match().then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setMatchState', {
                        key: 'matchList',
                        value: res.data.data
                    }),
                    resolve(res)
                }
            }).catch((error) => {
                reject(error)
            });
        })
    },
    getMatchDetails({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            match.matchDetails(data.id).then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setMatchState', {
                        key: 'matchDetails',
                        value: res.data.data
                    })
                }
                resolve(res)
            }).catch((error) => {
                reject(error)
            });
        })
    },
    getAboutMatchList({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            match.aboutMatch().then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setMatchState', {
                        key: 'aboutMatchList',
                        value: res.data.data
                    }),
                    resolve(res)
                }
            }).catch((error) => {
                reject(error)
            });
        })
    },
    getAboutMatchDetails({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            match.aboutMatchDetails(data.id).then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setMatchState', {
                        key: 'aboutMatchDetails',
                        value: res.data.data
                    })
                }
                resolve(res)
            }).catch((error) => {
                reject(error)
            });
        })
    },
}
export default {
    state,
    getters,
    actions,
    mutations
}